package com.lengxf.redis.redisTest;

import cn.hutool.core.collection.CollectionUtil;
import com.lengxf.redis.redisTest.dto.RedisDTO;
import com.lengxf.redis.service.RedisBitMapService;
import com.lengxf.redis.service.RedisGeoService;
import com.lengxf.redis.service.RedisHashService;
import com.lengxf.redis.service.RedisService;
import lombok.AllArgsConstructor;
import org.springframework.data.geo.Point;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * redis 测试
 *
 * @Author Lengxf
 */
@RestController
@RequestMapping
@AllArgsConstructor
public class RedisTestController {

    private final RedisService redisService;

    private final RedisHashService redisHashService;

    private final RedisBitMapService redisBitMapService;

    private final RedisGeoService redisGeoService;


    private final static RedisDTO redisDTO = new RedisDTO()
            .setString("string")
            .setADouble(1d)
            .setDate(new Date())
            .setAInteger(1)
            .setLocalDate(LocalDate.now())
            .setLocalDateTime(LocalDateTime.now());

    @PostMapping("set")
    public String set() {
        redisService.set(RedisKey.REDIS_TEST_KEY, redisDTO);
        return "success";
    }

    @PostMapping("hashSet")
    public String hashSet(@RequestParam("field") String field) {
        String sss = RedisKey.REDIS_TEST_KEY + field;
        redisHashService.hashSet(sss, field, redisDTO);
        return "success";
    }


    @PostMapping("expireKey")
    public Boolean expireKey() {
        return redisService.expireKey(RedisKey.REDIS_TEST_KEY, 60);
    }

    @PostMapping("setWithExpire")
    public String setWithExpire() {
        redisService.setWithExpire(RedisKey.REDIS_TEST_KEY, redisDTO, 60L);
        return "success";
    }

    @PostMapping("getExpire")
    public Long getExpire() {
        return redisService.getExpire(RedisKey.REDIS_TEST_KEY);
    }


    @PostMapping("get")
    public Object getValueTest() {
        return redisService.get(RedisKey.REDIS_TEST_KEY);
    }

    @PostMapping("hasKey")
    public Boolean hasKey() {
        return redisService.hasKey(RedisKey.REDIS_TEST_KEY);
    }

    @PostMapping("hashGet")
    public Object hashGet() {
        return redisHashService.hashGet(RedisKey.REDIS_TEST_KEY, "1");
    }

    @PostMapping("del")
    public Boolean del() {
        return redisService.remove(RedisKey.REDIS_TEST_KEY);
    }

    @PostMapping("hashKeys")
    public Object hashKeys() {
        String sss = RedisKey.REDIS_TEST_KEY + "*";
        return redisHashService.hashKeys(sss);
    }

    @PostMapping("hashKeysDel")
    public Object hashKeysDel() {
        String sss = RedisKey.REDIS_TEST_KEY;
        Set<String> strings = redisHashService.hashKeys(sss);
        if (CollectionUtil.isNotEmpty(strings)) {
            Object[] array = strings.toArray();
            return redisHashService.hashDel(RedisKey.REDIS_TEST_KEY, array);
        }
        return false;
    }


    @PostMapping("hashDel")
    public Boolean hashDel() {
        return redisHashService.hashDel(RedisKey.REDIS_TEST_KEY, new String[]{"1"});
    }


    @PostMapping("bitSet")
    public String bitSet(@RequestParam("key") String key,
                         @RequestParam("offset") Long offset) {
        redisBitMapService.set(RedisKey.REDIS_TEST_KEY + key, offset);
        return "success";
    }

    @PostMapping("bitGet")
    public Boolean bitGet(@RequestParam("key") String key,
                          @RequestParam("offset") Long offset) {
        return redisBitMapService.get(RedisKey.REDIS_TEST_KEY + key, offset);
    }

    @PostMapping("bitCount")
    public Long bitCount(@RequestParam("key") String key) {
        return redisBitMapService.bitCount(RedisKey.REDIS_TEST_KEY + key);
    }


    @PostMapping("savePoint")
    public Long savePoint() {
        Point point = new Point(104.072206, 30.663486);
        return redisGeoService.savePoint(RedisKey.REDIS_GEO_KEY, "10192", point);
    }

    @PostMapping("getPoint")
    public List<Point> getPoint() {
        return redisGeoService.getPoint(RedisKey.REDIS_GEO_KEY, "10192");
    }

}
